// miniprogram/pages/home/home.js

var a1 = wx.createInnerAudioContext()
var a2 = wx.createInnerAudioContext()
var a3 = wx.createInnerAudioContext()
//定时器编号
var timeoutNumber = 0
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:"",
    openType: 0, //开关状态
    temperature: 16, //温度
    modelType: 0, //空调模式
    dialogShow: false,
    buttons: [{
      text: '取消'
    }, {
      text: '确定'
    }],

  },
  inputEvent:function(e){
    console.log(e.detail)
    this.setData({
      name:e.detail.value
    })

  },
  openConfirm: function () {
    this.setData({
      dialogShow: true
    })
  },
  tapDialogButton(e) {
    console.log(e)
    if(e.detail.index == 0){
      this.setData({
        name:''
      })
    }
    this.setData({
      dialogShow: false,
      showOneButtonDialog: false
    })
  },
  tapOneDialogButton(e) {
    this.setData({
      showOneButtonDialog: true
    })
  },
  //调低温度
  clickSub() {
    this.d()
    if (this.data.temperature > 16) {
      this.setData({
        temperature: this.data.temperature - 1
      })
    } else {
      wx.showToast({
        icon: 'error',
        title: '已到最小温度啦',
      })
    }
  },
  //调高温度
  clickAdd() {
    this.d()
    if (this.data.temperature < 31) {
      this.setData({
        temperature: this.data.temperature + 1
      })
    } else {
      wx.showToast({
        icon: 'error',
        title: '已到最大温度啦',
      })
    }
  },

  /**
   * 选择模式
   */
  selectModel(e) {
    this.d()
    this.setData({
      modelType: e.currentTarget.dataset.t
    })
  },

  /**
   * 点击开关
   */
  clickSwitch() {
    this.d()
    this.setData({
      openType: !this.data.openType
    })
    if (this.data.openType == 1) {
      a2.play();
      timeoutNumber = setTimeout(() => {
        a3.play()
      }, 5000);
    } else {
      a2.stop();
      a3.stop();
      clearTimeout(timeoutNumber);
    }
  },

  //滴滴的声音
  d() {
    a1.play()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    a1.src = 'https://res.wx.qq.com/voice/getvoice?mediaid=MzUyOTA2NzAxNF8xMDAwMDExMDI='
    a2.src = 'https://res.wx.qq.com/voice/getvoice?mediaid=MzUyOTA2NzAxNF8xMDAwMDExMDM='
    a3.src = 'https://res.wx.qq.com/voice/getvoice?mediaid=MzUyOTA2NzAxNF8xMDAwMDExMDQ='
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '送给你一个空调，别客气热了就开哈~',
      imageUrl: '../../images/icons/a0.png'
    }
  },

  /**
   * 最重要的分享到朋友圈
   */
  onShareTimeline: function () {
    return {
      title: '给朋友圈的朋友们安排了空调，别客气热了就开哈~',
      imageUrl: 'https://wx.qlogo.cn/mmhead/Q3auHgzwzM7pTA1fEtrDbAw8OZoQ5m3XBsZBhusKxGpFMlmAwNpm3g/0'
    }
  }
})